﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using Microsoft.Practices.Composite.Presentation.Commands;
using AuthenticationService.Client;
using System.Security.Principal;
using System.Windows.Ria.ApplicationServices;
using Microsoft.Practices.Composite.Events;
using UserManager.Module.Views;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Regions;
using Infrastructure;

namespace UserManager.Module.ViewModels
{
    public class LoginStatusViewModel : INotifyPropertyChanged
    {
        WebContext context;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;

        public LoginStatusViewModel(IUnityContainer container, IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            _container = container;
            _eventAggregator = eventAggregator;
            _regionManager = regionManager;
            LogonCommand = new DelegateCommand<object>(Logon, CanLogon);
            LogoutCommand = new DelegateCommand<object>(Logout, CanLogout);
            context = WebContext.Current;
            context.Authentication.LoggedIn += new EventHandler<System.Windows.Ria.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedIn);
            context.Authentication.LoggedOut += new EventHandler<System.Windows.Ria.ApplicationServices.AuthenticationEventArgs>(Authentication_LoggedOut);
        }

        void Authentication_LoggedOut(object sender, System.Windows.Ria.ApplicationServices.AuthenticationEventArgs e)
        {
            IsAuthenticated = false;
            NotifyPropertyChanged("User");
        }

        void Authentication_LoggedIn(object sender, System.Windows.Ria.ApplicationServices.AuthenticationEventArgs e)
        {
            IsAuthenticated = true;
            NotifyPropertyChanged("User");
        }

        private bool isAuthenticated = false;
        public bool IsAuthenticated 
        {
            get
            {
                return isAuthenticated;
            }
            set
            {
                isAuthenticated = value;
                NotifyPropertyChanged("IsAuthenticated");
            }
        }

        public IPrincipal User
        {
            get
            {
                return context.User;
            }
        }

        #region Commands
        public DelegateCommand<object> LogonCommand { get; private set; }

        public void Logon(object commandArg)
        {
            LoginView loginView = _container.Resolve<LoginView>("LoginWindow");
            loginView.Show();
        }

        public bool CanLogon(object commandArg)
        {
            return true;
        }

        public DelegateCommand<object> LogoutCommand { get; private set; }

        public void Logout(object commandArg)
        {
            context.Authentication.Logout(true);
        }

        public bool CanLogout(object commandArg)
        {
            return true;
        }
        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
